package com.quanyan.user.web;

import com.quanyan.api.APIResponse;
import com.quanyan.user.service.UserFollowService;
import com.quanyan.user.service.UserFriendService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 用户关系
 * Created by huadongyang on 2016/11/18.
 */
@RestController
@RequestMapping(value = "/api/user/relationship")
public class UserFriendController {
    private static final Logger logger = LoggerFactory.getLogger(UserFriendController.class);

    @Autowired
    private UserFriendService userFriendService;

    @Autowired
    private UserFollowService  userFollowService;

    /**
     * 关注用户
     *
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "/followUser", method = RequestMethod.POST, produces = "application/json")
    public APIResponse followUser(@RequestBody Map<String, Integer> map, HttpServletRequest request) {
        Integer followUid = map.get("followUid");
        Integer uid = (Integer) request.getAttribute("uid");

        if (followUid == null || uid == null) {
            return APIResponse.returnFail("请传入有效参数！");
        }

        try {
            return userFollowService.followUser(uid, followUid);
        } catch (Exception e) {
            logger.error("关注该用户失败；{}", e.getMessage());
            return APIResponse.returnFail("关注该用户失败！");
        }
    }

    /**
     * 取消关注用户
     *
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "/cancelFollowUser", method = RequestMethod.POST, produces = "application/json")
    public APIResponse cancelFollowUser(@RequestBody Map<String, Integer> map, HttpServletRequest request) {
        Integer followUid = map.get("followUid");
        Integer uid = (Integer) request.getAttribute("uid");

        if (followUid == null || uid == null) {
            return APIResponse.returnFail("请传入有效参数！");
        }

        try {
            return userFollowService.cancelFollowUser(uid, followUid);
        } catch (Exception e) {
            logger.error("取消用户关注失败；{}", e.getMessage());
            return APIResponse.returnFail("取消用户关注失败！");
        }
    }

}
